package service

import (
	"errors"
	"fmt"
	"go.uber.org/zap"
	"iot-base/common/cache"
	"iot-base/common/ctrltype"
	"iot-base/common/logger"
	"iot-base/common/notify"
	"regexp"
	"strconv"
	"strings"
	"time"
)

type Co struct {
}

type CoLogic struct {
}

func NewCoLogic() *CoLogic {
	return &CoLogic{}
}

// Co 一氧化碳
func (this *CoLogic) Co(id string, thing *cache.ThingInfo, v interface{}, round int64) (interface{}, error) {
	ip := thing.GetThingParam("ip", "")
	port := thing.GetThingParam("port", "")
	coStr, err := SynHub.SendStrData(ip+":"+port, "coochr$")
	if strings.Contains(coStr, "error") {
		logger.Log.Error("Co service.SynHub.SendStrData err:" + coStr)
		fmt.Println("coochr$", coStr)
		return nil, err
	}
	logger.Log.Info("Co coochr$ " + coStr)
	fmt.Println("coochr$ info:=", coStr, err)
	if err != nil {
		logger.Log.Error("Co service.SynHub.SendStrData err:", zap.Error(err))
		return nil, err
	}
	coArr := strings.Split(coStr, "=")
	var status float64 = 1
	switch coArr[0][0:1] {
	case "*":
		status = 2
	case "#":
		status = 3
	}
	reg := regexp.MustCompile("[\\-]?[\\d]+\\.?[\\d]+")
	data := reg.Find([]byte(coArr[1]))
	now := time.Now().Unix()
	co, _ := strconv.ParseFloat(string(data), 64)
	notify.RawData(thing.Thing.UID, "co", "", 0, co, now, now)
	notify.RawData(thing.Thing.UID, "status", "", 0, status, now, now)
	notify.RawData(thing.Thing.UID, "coValueStatus", "", 0, fmt.Sprintf("%s%v", coArr[0][0:1], co), now, now)

	// 获取详细状态.
	cootwc, err := SynHub.SendStrData(ip+":"+port, "cootwc$")
	fmt.Println("cootwc", cootwc, err)
	if err != nil {
		logger.Log.Error("Co service.SynHub.SendStrData err:", zap.Error(err))
		return nil, err
	}
	logger.Log.Info("Co cootwc " + cootwc)
	if strings.Contains(cootwc, "error") {
		logger.Log.Error("Co cootwc$ service.SynHub.SendStrData err:" + cootwc)
		fmt.Println("cootwc$", cootwc)
		return nil, err
	}

	cootwc = cootwc[:len(cootwc)-1]
	cootwcArr := strings.Split(cootwc, ",")
	// 上报数据.
	ReportDataFloat64(thing.Thing.UID, cootwcArr, CoField)
	return nil, nil
}

var CoField = map[int]string{
	1: "pmt_v",
	2: "canbi_v",
	3: "press",
	4: "flow",
	5: "temp",
	6: "xg_temp",
}

func (this *CoLogic) CoSv(pCmd *ctrltype.CtrlCmd, tingInfo *cache.ThingInfo) (map[string]interface{}, error) {
	ip := tingInfo.GetThingParam("ip", "")
	port := tingInfo.GetThingParam("port", "")
	var requestMap map[string]interface{}
	requestMap = make(map[string]interface{})
	switch pCmd.ID {
	case "czeros":
		readbuffer, err := SynHub.SendStrData(ip+":"+port, "czeros$")
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "ok") {
			requestMap["msg"] = "ok"
		} else {
			requestMap["msg"] = "fail:" + readbuffer
		}
		return requestMap, nil
	case "czeroe":
		readbuffer, err := SynHub.SendStrData(ip+":"+port, "czeroe$")
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "ok") {
			requestMap["msg"] = "ok"
		} else {
			requestMap["msg"] = "fail:" + readbuffer
		}
		return requestMap, nil
	case "czeroq":
		readbuffer, err := SynHub.SendStrData(ip+":"+port, "czeroq$")
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "ok") {
			requestMap["msg"] = "ok"
		} else {
			requestMap["msg"] = "fail:" + readbuffer
		}
		return requestMap, nil
	case "cspans":
		concentration, ok := pCmd.Params["concentration"].(string)
		if !ok {
			return nil, errors.New("concentration type is err")
		}
		if len(concentration) < 3 {
			for i := 0; i < 4-len(concentration); i++ {
				concentration = "0" + concentration
			}
		}
		readbuffer, err := SynHub.SendStrData(ip+":"+port, "cspans "+concentration+"$")
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "ok") {
			requestMap["msg"] = "ok"
		} else {
			requestMap["msg"] = "fail:" + readbuffer
		}
		return requestMap, nil
	case "cspane":
		concentration, ok := pCmd.Params["concentration"].(string)
		if !ok {
			return nil, errors.New("concentration type is err")
		}
		if len(concentration) < 3 {
			for i := 0; i < 4-len(concentration); i++ {
				concentration = "0" + concentration
			}
		}
		readbuffer, err := SynHub.SendStrData(ip+":"+port, "cspane "+concentration+"$")
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "ok") {
			requestMap["msg"] = "ok"
		} else {
			requestMap["msg"] = "fail:" + readbuffer
		}
		return requestMap, nil
	case "cspanq":
		concentration, ok := pCmd.Params["concentration"].(string)
		if !ok {
			return nil, errors.New("concentration type is err")
		}
		if len(concentration) < 3 {
			for i := 0; i < 4-len(concentration); i++ {
				concentration = "0" + concentration
			}
		}
		readbuffer, err := SynHub.SendStrData(ip+":"+port, "cspanq "+concentration+"$")
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "ok") {
			requestMap["msg"] = "ok"
		} else {
			requestMap["msg"] = "fail:" + readbuffer
		}
		return requestMap, nil
	case "status":
		readbuffer, err := SynHub.SendStrData(ip+":"+port, "coochr$")
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "error") {
			logger.Log.Error("co service.SynHub.SendStrData err:" + readbuffer)
			fmt.Println("coochr$", readbuffer)
			return nil, err
		}
		logger.Log.Info("co coochr$ info:=" + readbuffer)
		fmt.Println("coochr$ info:=", readbuffer, err)
		if err != nil {
			logger.Log.Error("co service.SynHub.SendStrData err:", zap.Error(err))
			return nil, err
		}
		switch readbuffer[:1] {
		case "*":
			requestMap["msg"] = "2"
		case "#":
			requestMap["msg"] = "3"
		default:
			requestMap["msg"] = "1"
		}
		return requestMap, nil
	case "value":
		readbuffer, err := SynHub.SendStrData(ip+":"+port, "coochr$")
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "error") {
			logger.Log.Error("co service.SynHub.SendStrData err:" + readbuffer)
			fmt.Println("coochr$", readbuffer)
			return nil, err
		}
		logger.Log.Info("co coochr$ info:=" + readbuffer)
		fmt.Println("coochr$ info:=", readbuffer, err)
		if err != nil {
			logger.Log.Error("co service.SynHub.SendStrData err:", zap.Error(err))
			return nil, err
		}
		data := strings.Split(readbuffer, "=")
		if len(data) != 2 {
			return nil, errors.New("return data is err:" + readbuffer)
		}
		data[1] = strings.Replace(data[1], "$", "", -1)
		if data[1][0] == '*' || data[1][0] == '#' {
			requestMap["value"] = data[1][1:]
		} else {
			requestMap["value"] = data[1]
		}
		return requestMap, nil
	case "his":
		start, ok := pCmd.Params["start"].(string)
		if !ok {
			return nil, errors.New("start type is err")
		}
		end, ok := pCmd.Params["end"].(string)
		if !ok {
			return nil, errors.New("end type is err")
		}
		readbuffer, err := SynHub.SendStrData(ip+":"+port, "coohis"+start+" "+end+"$")
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "error") {
			logger.Log.Error("co service.SynHub.SendStrData err:" + readbuffer)
			fmt.Println("coohis", readbuffer)
			return nil, err
		}
		requestMap["result"] = readbuffer
		return requestMap, nil
	default:
		return nil, errors.New("no service")
	}

}
